//
//  NickNameViewController.m
//  Paixiu
//
//  Created by admin on 2021/9/29.
//

#import "NickNameViewController.h"
#import <YMIMKit/YMIMKit.h>
#import "UIViewController+HUD.h"
#import <Masonry/Masonry.h>
@interface NickNameViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UIButton *sureButtton;
@property (nonatomic,strong)UILabel *nameLable;
@property (nonatomic,strong)UITextField *nameText;
@property (nonatomic,strong)UIView *nameLine;
@end

@implementation NickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改好友备注";
    self.nameText.text = @"";
    [self.nameText becomeFirstResponder];
    [self initView];
    // Do any additional setup after loading the view.
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.nameText resignFirstResponder];
}
#pragma mark 保存昵称
-(void)sureAction{
    
    if ([self.nameText.text isEqualToString:@""]) {
        
        [self showHint:@"好友备注不能为空"];
        return;
    }
    if (self.nameText.text.length>6) {
        
        [self showHint:@"好友备注超出字数限制，请重新输入"];
        
        return;
    }

    [[YMIMClient sharedClient].friendManager addFriendNoteWithFriendId:self.tid note:self.nameText.text completion:^(YMErrorCode code) {
        
        self.modifyNickName(self.nameText.text);
        [self.navigationController popViewControllerAnimated:YES];
        [self showHint:@"昵称修改成功"];
        
    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}
-(void)initView{
    
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]
                                  initWithCustomView:self.sureButtton];
    self.navigationItem.rightBarButtonItem = doneItem;
    [self.view addSubview:self.nameLable];
    [self.view addSubview:self.nameText];
    [self.view addSubview:self.nameLine];
    [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.view.mas_left).offset(15);
        make.top.mas_equalTo(self.view.mas_top).offset(20);
            
    }];
    
    [self.nameText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self.nameLable.mas_left);
        make.right.mas_equalTo(self.view.mas_right).offset(-60);
        make.top.mas_equalTo(self.nameLable.mas_bottom).offset(35);
        
        
    }];
    
    [self.nameLine mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_equalTo(self.nameText);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.nameText.mas_bottom).offset(5);
        
    }];
}
-(UIView *)nameLine{
    
    if (_nameLine == nil) {
        
        _nameLine = [[UIView alloc]init];
        _nameLine.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    }
    
    return _nameLine;
}
-(UITextField *)nameText{
    
    if (_nameText == nil) {
        
        _nameText = [[UITextField alloc]init];
        _nameText.placeholder = @"请输入好友备注";
        _nameText.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _nameText.font = [UIFont systemFontOfSize:16];
        _nameText.delegate = self;
    }
    
    return _nameText;
}
-(UILabel *)nameLable{
    
    if (_nameLable == nil) {
        
        _nameLable = [[UILabel alloc]init];
        _nameLable.text = @"修改好友备注";
        _nameLable.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _nameLable.font = [UIFont boldSystemFontOfSize:16];
    }
    
    return _nameLable;
}
-(UIButton *)sureButtton{
    
    if (_sureButtton == nil) {
        
        _sureButtton = [[UIButton alloc]init];
        [_sureButtton setTitle:@"确定" forState:UIControlStateNormal];
        _sureButtton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_sureButtton setTitleColor:[UIColor colorWithRed:72/255.0 green:142/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal];
        [_sureButtton addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _sureButtton;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
